package k2_oop.p9_collection.demo06;

public class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    /**
     * 先按年龄倒序排
     * 如果年龄相同 按姓名升序排
     * @param o the object to be compared.
     * @return
     */
    @Override
    public int compareTo(Student o) {

        String thisName = this.name;
        String thatName = o.getName();

        int thisAge = this.age;
        int thatAge = o.getAge();

        //如果年龄相同 按照姓名升序排列
        if(thisAge==thatAge){
            return thatName.compareTo(thisName);
        }else{
            return thatAge-thisAge;
        }
    }
}
